#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int inc(int i, int n);
int profit(int **C, int i, int j, int siz, int jsiz, int mrow, int ncol);
int maxprofit(int **C, int siz, int jsiz, int mrow, int ncol);

int** createMatrix(int mrow, int ncol);
void freeMatrix(int **C, int mrow, int ncol);
int printMatrix(int **C, int mrow, int ncol);

int torus_lot ( int **C, int M, int N );

int main()
{
    int **C;

    int M = 5;
    int N = 7;

//    int A[][3] = {{1, -1, 2}, {-1, -1, -1}, {3, -1, 4} };
//    int ***C = &A;

    C = createMatrix(M, N);
    printMatrix(C, M, N);

//    int test = profit(C, 2, 1, 2, M, N);

    //int test2 = maxprofit(*C, 2, M, N);

    int test3 = torus_lot(C, M, N);

//    printf("profit() = %d\n", test);
//    printf("mProfit() = %d\n", test2);
    printf("torus_lot() = %d\n", test3);

    freeMatrix(C, M, N);

    printf("Hello, World\n");
    printf("inc() = %d\n", inc(9, 10));

    return 0;
}

//-----------------------------------------------------------------------------
int inc(int i, int n)
{
    if (++i<n) return(i);
    else return 0;
}

int maxprofit(int **C, int isiz, int jsiz, int mrow, int ncol)
{
    int i, j;
    int tmp;
    int mProfit = 0;
    for (i = 0; i < mrow; i++)
    {
        for (j = 0; j < ncol; j++)
        {
            tmp = profit(C, i, j, isiz, jsiz, mrow, ncol);
            if (mProfit < tmp) mProfit = tmp;
        }
    }
    return mProfit;
}

int profit(int **C, int i, int j, int isiz, int jsiz, int mrow, int ncol)
{
    int iz, jz, jj;
    int iprofit = 0;
    for (iz = 0; iz < isiz; iz++)
    {
        jj = j;
        for(jz = 0; jz < jsiz; jz++)
        {
            iprofit += C[i][jj];
            //iprofit += (C[i][jj] > 0 ? C[i][jj] : 0);
            jj = inc(jj, ncol);
        }
        i = inc(i, mrow);
    }
    return iprofit;
}

int torus_lot ( int **C, int M, int N ) {
    int isiz, jsiz;
    int maxP = 0;
    int tmp;

    for (isiz = 1; isiz <= M; isiz++)
    {
        for (jsiz = 1; jsiz <= N; jsiz++)
        {
            tmp = maxprofit(C, isiz, jsiz, M, N);
            if (maxP < tmp) maxP = tmp;
        }
    }

    return maxP;
}

//-----------------------------------------------------------------------------


//[
//    [ 3, -10, -5,  -6,   5,  4,  8, -9]
//    [-2, -10, -6,   0, -10, -6,  3,  1]
//    [-6,   2,  6, -10,   6,  4, -3, -7]
//    [10,  -3, -9,  -8,   7,  2,  6,  5]
//    [ 1,  10, -3,   1,   7,  2,  8,  2]
//    [ 4, -10, -6,  -4,  -9, -6, -8, -5]
//]

//[
//    [ 3, -10,  -5, -6,  5,   4,  8],
//    [-9,  -2, -10, -6,  0, -10, -6],
//    [ 3,   1,  -6,  2,  6, -10,  6],
//    [ 4,  -3,  -7, 10, -3,  -9, -8],
//    [ 7,   2,   6,  5,  1,  10, -3]
//]

int** createMatrix(int mrow, int ncol)
{
    int i, j;
    int **C;
    C = (int**) malloc(mrow * sizeof(int*));
    for (i = 0; i < mrow; i++)
    {
        *(C+i) = (int*) malloc(sizeof(int) * ncol);    //malloc nCol items of a row.
    }

//---------------------5x7 = 24 but expected 35------------------------------------------------
  C[0][0] = 3;     C[0][1] = -10;   C[0][2] = -5;      C[0][3] = -6;      C[0][4] = 5;     C[0][5] = 4;      C[0][6] = 8;
  C[1][0] = -9;    C[1][1] = -2;    C[1][2] = -10;     C[1][3] = -6;      C[1][4] = 0;     C[1][5] = -10;    C[1][6] = -6;
  C[2][0] = 3;     C[2][1] = 1;     C[2][2] = -6;      C[2][3] = 2;       C[2][4] = 6;     C[2][5] = -10;    C[2][6] = 6;
  C[3][0] = 4;     C[3][1] = -3;    C[3][2] = -7;      C[3][3] = 10;      C[3][4] = -3;    C[3][5] = -9;     C[3][6] = -8;
  C[4][0] = 7;     C[4][1] = 2;     C[4][2] = 6;       C[4][3] = 5;       C[4][4] = 1;     C[4][5] = 10;     C[4][6] = -3;

//---------------------6x8 = 41------------------------------------------------
//  C[0][0] = 3;     C[0][1] = -10;   C[0][2] = -5;     C[0][3] = -6;     C[0][4] = 5;     C[0][5] = 4;    C[0][6] = 8;    C[0][7] = -9;
//  C[1][0] = -2;    C[1][1] = -10;   C[1][2] = -6;     C[1][3] = 0;      C[1][4] = -10;   C[1][5] = -6;   C[1][6] = 3;    C[1][7] = 1;
//  C[2][0] = -6;    C[2][1] = 2;     C[2][2] = 6;      C[2][3] = -10;    C[2][4] = 6;     C[2][5] = 4;    C[2][6] = -3;   C[2][7] = -7;
//  C[3][0] = 10;    C[3][1] = -3;    C[3][2] = -9;     C[3][3] = -9;     C[3][4] = 7;     C[3][5] = 2;    C[3][6] = 6;    C[3][7] = 5;
//  C[4][0] = 1;     C[4][1] = 10;    C[4][2] = -3;     C[4][3] = 1;      C[4][4] = 7;     C[4][5] = 2;    C[4][6] = 8;    C[4][7] = 2;
//  C[5][0] = 4;     C[5][1] = -10;   C[5][2] = -6;     C[5][3] = -4;     C[5][4] = -9;    C[5][5] = -6;   C[5][6] = -8;   C[5][7] = -5;

//---------------------7x4 = 17 but expected 24--------------------------------
//  C[0][0] = 3;      C[0][1] = -10;   C[0][2] = -5;    C[0][3] = -6;
//  C[1][0] = 5;      C[1][1] = 4;     C[1][2] = 8;     C[1][3] = -9;
//  C[2][0] = -2;     C[2][1] = -10;   C[2][2] = -6;    C[2][3] = 0;
//  C[3][0] = -10;    C[3][1] = -6;    C[3][2] = 3;     C[3][3] = 1;
//  C[4][0] = -6;     C[4][1] = 2;     C[4][2] = 6;     C[4][3] = -10;
//  C[5][0] = 6;      C[5][1] = 4;     C[5][2] = -3;    C[5][3] = -7;
//  C[6][0] = 10;     C[6][1] = -3;    C[6][2] = -9;    C[6][3] = -8;

//---------------------3x3 = 10------------------------------------------------
//  C[0][0] = 1;   C[0][1] = -1;   C[0][2] = 2;
//  C[1][0] = -1;   C[1][1] = -1;   C[1][2] = -1;
//  C[2][0] = 3;    C[2][1] = -1;   C[2][2] = 4;
//--------------------------mrow x ncol ---------------------------------------
//    for (i = 0; i < mrow; i++)
//    {
//        for (j = 0; j < ncol; j++)
//        {
//            C[i][j] = rand()%100 - 50;
//            //printf("C[%d][%d] = %d\n", i, j,C[i][j]);
//        }
//    }
//-----------------------------------------------------------------------------
    return C;
}

void freeMatrix(int **C, int mrow, int ncol)
{
    int i;
    for (i = 0; i < mrow; i++)
    {
        free(C[i]);
    }
    free(C);
}

int printMatrix(int **C, int mrow, int ncol)
{

    printf("printMatrix(**C, %d, %d) \n", mrow, ncol);
    printf("C[%d][%d] = %d\n", 0, 0, C[0][0]);
    int i, j;
    for (i = 0; i < mrow; i++)
    {
        for (j = 0; j < ncol; j++)
        {
            printf("C[%d][%d] = %d\n", i, j, C[i][j]);
        }
    }
    return 0;
}
